package com.bdzl.biz.controller;

import com.bdzl.biz.service.AgentService;
import com.bdzl.biz.servicefeign.AdminBankcardFeignService;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.data.EnterpriseDO;
import com.bdzl.pub.entity.loan.AgentDO;
import com.bdzl.pub.entity.loan.AgentRelBankcardDO;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.data.AgentVO;
import com.bdzl.pub.vo.data.BankcardVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 经销商信息表
 *
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-05-11 15:23:42
 */

@Controller
@RequestMapping("/bdzl/agent")
public class AgentController extends BaseController {
	@Autowired
	private AgentService agentService;
	@Autowired
	private AdminBankcardFeignService adminBankcardFeignService;


	@GetMapping()
	String Agent(){
	    return "bdzl/agent/agent";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<AgentDO> agentList = agentService.list(query);
		int total = agentService.count(query);
		PageUtils pageUtils = new PageUtils(agentList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add(){
	    return "bdzl/agent/add";
	}

	@GetMapping("/edit/{agentId}")
	String edit(@PathVariable("agentId") Integer agentId,Model model){
		AgentDO agent = agentService.get(agentId);
		model.addAttribute("agent", agent);
	    return "bdzl/agent/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( AgentDO agent){
		if(agentService.save(agent)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( AgentDO agent){
		agentService.update(agent);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer agentId){
		if(agentService.remove(agentId)>0){
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] agentIds){
		agentService.batchRemove(agentIds);
		return R.ok();
	}

	/**
	 * 添加经销商信息
	 */
	@PostMapping("/addAgent")
	@ResponseBody
	public R addAgent(@RequestBody  @Validated AgentVO agentVO) throws Exception{
		agentService.addAgent(agentVO,super.getUserId());
		return R.ok();
	}

	/**
	 * 添加经销商信息
	 */
	@GetMapping("/getAgent/{agentId}")
	@ResponseBody
	public R getAgent(@PathVariable("agentId") Integer agentId) throws Exception{
		return R.ok(agentService.getAgent(agentId));
	}

	@ResponseBody
	@GetMapping("/listAgent")
	public PageUtils listAgent(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<AgentVO> agentList = agentService.getAgentAndUserList(query);
		int total = 0;
		query.put("offset",null);
		query.put("limit",null);
		List<AgentVO> totalList = agentService.getAgentAndUserList(query);
		if(totalList!=null){
			total = totalList.size();
		}

		PageUtils pageUtils = new PageUtils(agentList, total);
		return pageUtils;
	}


	/**
	 * 修改经销商信息
	 */
	@PostMapping("/updateAgnet")
	@ResponseBody
	public R updateAgnet(@RequestBody AgentVO agentVO) throws Exception{
		agentService.updateAgnet(agentVO,super.getUserId());
		return R.ok();
	}

	/**
	 * 删除银行卡信息
	 * @param bcId     银行卡ID
	 * @param arbAgent 经销商ID
	 * @return R
	 */
	@DeleteMapping("/bancrad")
	@ResponseBody
	public R deleteBancrad(Integer bcId, Integer arbAgent) {
		agentService.deleteBancrad(bcId,arbAgent);
		return R.ok();
	}

	/**
	 * 添加营业执照
	 */
	@ResponseBody
	@PostMapping("/saveEnterprise")
	public R saveEnterprise(@RequestBody @Validated EnterpriseDO enterprise){
		agentService.saveEnterprise(enterprise,super.getUserId());
		return R.ok();
	}

	/**
	 * 查询经销商和银行卡信息
	 */
	@GetMapping("/getBancardList")
	@ResponseBody
	public PageUtils getBancardList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<BankcardVO> bankcardVOList = agentService.getBankcardList(query);
		Integer count = agentService.getBankcardListcount(query);
		PageUtils pageUtils = new PageUtils(bankcardVOList, count);
		return pageUtils;
	}

	/**
	 * 修改银行卡信息
	 */
	@PostMapping("/updateBanckcard")
	@ResponseBody
	public R updateBanckcard(@RequestBody BankcardVO bankcardVO){
		return agentService.updateBankcard(bankcardVO,super.getUserId());
	}


}
